<template>
  <div class="rightlist">
    <el-card>
      <el-form
        :model="form"
        :rules="formrules"
        ref="formRef"
        label-width="70px"
      >
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
      </el-form>

      <el-table :data="rightlist" stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="权限名称" prop="authName"></el-table-column>
        <el-table-column label="路径" prop="path"></el-table-column>
        <el-table-column label="权限等级" prop="level">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.level === 0">一级权限</el-tag>
            <el-tag v-if="scope.row.level === 1" type="success"
              >二级权限</el-tag
            >
            <el-tag v-if="scope.row.level === 2" type="warning"
              >三级权限</el-tag
            >
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="修改用户"
        :visible.sync="show"
        width="50%"
        @close="editDialo"
      >
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "rightlist",
  data() {
    return {
      show: false,
      rightlist: [],
      form: {
        username: "",
        email: "",
        mobile: ""
      },
      formrules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur"
          }
        ],
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            validator: checkMobile,
            message: "手机号码不正确，请重新输入",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getrightlist();
  },
  methods: {
    async getrightlist() {
      let { data: res } = await this.$axios.get("rights/list");
      if (res.meta.status !== 200)
        return this.$message.error("获取权限列表失败");
      this.rightlist = res.data;
    },
    close() {
      this.$refs.formRef.resetFields();
    },
    changeuser() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return this.$message.error("请填写完整用户信息");
        let { data: res } = await this.$axios.put(
          "/users/" + this.form.id,
          this.form
        );
        if (res.meta.status !== 200) return this.$message.error("修改用户失败");
        this.$message.success("修改用户成功");
        this.show = false;
        this.getlist();
      });
    },
    async deluser(id) {
      let confirmResult = await this.$confirm(
        "请问是否要永久删除该用户",
        "删除提示",
        {
          confirmButtonText: "确认删除",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已经取消删除");
      }
      let { data: res } = await this.$axios.delete("/users/" + id);
      if (res.meta.status !== 200) return this.$message.error("删除用户失败");
      this.$message.success("删除用户成功");
      this.getlist();
    }
  }
};
</script>

<style lang="scss" scoped>
.rightlist {
  width: 100%;
}
</style>